﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using Antu.WebControls;
using Antu.Core.Components;
using Antu.Core.Services;
using System.Web.UI.WebControls;
using EluZSProvider;
using System.Web;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Data;

namespace WebSys.UI.Control.ZhengShou
{
    public class ZS_FXPGBBUPControl : MPBasicTemplate
    {
        EluZSDataProvider _EluZSDataProvider = (EluZSDataProvider)ProviderService.ProviderServiceMange.GetProvider("EluZSProvider");
        CustomGridView CustomGridView1;
        Button upfile;
        DropDownList DropDownList1;
        TextBox TextBox1, TextBox2;
        Literal Literal1;
        public string strPID = string.Empty;
        public string strUseType = string.Empty;
        public string itemname = string.Empty;
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            CustomGridView1 = (CustomGridView)FindControl("CustomGridView1");
            DropDownList1 = (DropDownList)FindControl("DropDownList1");
            TextBox1 = (TextBox)FindControl("TextBox1");
            TextBox2 = (TextBox)FindControl("TextBox2");
            upfile = (Button)FindControl("upfile");
            Literal1 = (Literal)FindControl("Literal1");
            strPID = base.Page.Request["PID"];
            itemname = DropDownList1.SelectedValue;
            strUseType = TextBox1.Text;
            if (!base.Page.IsPostBack)
            {
                BindData();
            }
            upfile.Click += new EventHandler(upfile_Click);
            CustomGridView1.RowDeleting += new GridViewDeleteEventHandler(CustomGridView1_RowDeleting);
            CustomGridView1.OnLoadData += new EventHandler(CustomGridView1_OnLoadData);
        }
        protected void BindData()
        {
            if (!string.IsNullOrEmpty(strPID))
            {
                Literal1.Text = _EluZSDataProvider.GetString("select 项目名称 from zs_project where id='" + strPID + "'");
                string sql = "select * from ZS_FILE where itemname in (" + TextBox2.Text.Trim() + ") ";
                if (!string.IsNullOrEmpty(strPID)) { sql += " and ITEMID='" + strPID + "' "; }
                DataTable dt = _EluZSDataProvider.GetTable(sql);
                CustomGridView1.DataSource = dt;
                CustomGridView1.PageSize = 10;
                CustomGridView1.RecordCount = dt.Rows.Count;
                CustomGridView1.DataKeyNames = new string[] { "ID" };
                CustomGridView1.DataBind();
            }
        }

        protected void CustomGridView1_OnLoadData(object sender, EventArgs e)
        {
            BindData();
        }
        protected void CustomGridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            string fjid = CustomGridView1.DataKeys[e.RowIndex].Value.ToString();
            bool del = _EluZSDataProvider.DeleteXmByID("ZS_FILE", "ID", fjid);
            if (del)
            {
                BindData();
                Page.ClientScript.RegisterStartupScript(this.GetType(), "", "alert('删除成功！')", true);
            }
        }
        protected void upfile_Click(object sender, EventArgs e)
        {
            FileUpload picFile = (FileUpload)FindControl("picFile");
            TextBox filename = (TextBox)FindControl("filename");
            if (picFile.HasFile)
            {
                HttpPostedFile postfile = picFile.PostedFile;
                string fileName = Path.GetFileName(postfile.FileName);
                string fileext = fileName.Substring(fileName.LastIndexOf("."));
                if ((fileext.ToLower() == ".jpg") || (fileext.ToLower() == ".gif") || (fileext.ToLower() == ".bmp") || (fileext.ToLower() == ".jpeg")
                    || (fileext.ToLower() == ".doc") || (fileext.ToLower() == ".docx") || (fileext.ToLower() == ".xls"))
                {
                    try
                    {
                        if (!string.IsNullOrEmpty(postfile.FileName))
                        {
                            Stream filestream = postfile.InputStream;
                            byte[] bFileContent = new byte[filestream.Length];
                            string tt = Convert.ToBase64String(bFileContent);

                            if (bFileContent.Length > 5242880)
                            {
                                HttpContext.Current.Response.Write("<script>alert('上传文件大于5M！');window.location.href=window.location.href;</script>");
                            }
                            else
                            {
                                if (bFileContent.Length > 0)
                                {
                                    filestream.Read(bFileContent, 0, bFileContent.Length);
                                    string guidstr = Guid.NewGuid().ToString().Replace("-", "").ToUpper();
                                    string filenametemp = filename.Text;
                                    if (string.IsNullOrEmpty(filenametemp))
                                    {
                                        filenametemp = fileName.Replace(fileext, "");
                                    }
                                    _EluZSDataProvider.UpdateFileContent("ZS_FILE", guidstr, bFileContent, bFileContent.Length, strPID, filenametemp, fileext, itemname, strUseType);
                                    HttpContext.Current.Response.Write("<script>alert('文件上传成功！');window.location.href=window.location.href;</script>");
                                }
                            }
                        }
                        else
                        {
                            HttpContext.Current.Response.Write("<script>alert('文件名不得为空！');history.back();</script>");
                        }
                    }
                    catch
                    {
                        HttpContext.Current.Response.Write("<script>alert('上传出错！');history.back();</script>");
                    }
                }
                else
                {
                    HttpContext.Current.Response.Write("<script>alert('上传文件只支持jpg,gif,jpeg,bmp,doc,docx,xls格式');history.back();</script>");
                }
            }
            else
            {
                HttpContext.Current.Response.Write("<script>alert('请选择上传文件！');history.back();</script>");
            }
        }
    }
}


